var express = require('express');
var router = express.Router();
//引入上传模块
var multer = require('multer');
//上传的配置
var upload = multer({dest:"./public/upload"});
var fs = require('fs')
var sqlQuery = require('../../../module/sql');


//电影是否上热门
router.post('/hot',async(req,res)=>{
  let filmid = req.body.id
  let hot = req.body.hot
  let sqlStr = "update film set hot=? where id=?";
  let result = await sqlQuery(sqlStr,[hot,filmid])
  res.json({
    state:"ok",
    content:Array.from(result)
  })
})


//电影状态更改(上架或者下架)
router.post('/state',async(req,res)=>{
  console.log(req.body)
  let sid = req.body.id
  let state = req.body.state
  // console.log("sid:",sid)
  // console.log("state:",state)
  let sqlStr1 = "update film set state=? where id=?";
  let result1 = await sqlQuery(sqlStr1,[state,sid])
  res.json({
    state:"ok",
    content:Array.from(result1)
  })
})




// router.post('/',upload.single('poster'), async(req, res, next)=> {
//     let filmid = req.body.filmid
//     let result = rename(req)
//     //将改名后的结果，上传到数据库
//     let strSql = "update film set poster = ? where id = ?";
//     await sqlQuery(strSql,[result.imgUrl,filmid])
//     res.json(result)
//   })
  
//   //改名函数
//   function rename(req) { 
//     //重命名操作
//     let oldPath = req.file.destination+"/"+req.file.filename;
//     let newPath = req.file.destination+"/"+req.file.filename+req.file.originalname;
//     fs.rename(oldPath,newPath,()=>{
//       //console.log("重命名成功")
//     })
//     return {
//       state:'ok',
//       imgUrl:"/upload/"+req.file.filename+req.file.originalname
//     }
//    }
  


  module.exports = router;